﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Products;
using MangoMes.Commont.Domain.Purchase;
using MangoMes.Commont.Domain.Suppliers;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MathNet.Numerics.Distributions;
using MediatR;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace MangoMes.Write.API.Applications.PurchaseStorags
{
    public class ProductListHandler : IRequestHandler<ProductListCommand, ResultsDTO<ProductManage>>
    {
        private readonly IBateRepository<ProductManage> prdto;
        private readonly ILogger<ProductListHandler> logger;
        public ProductListHandler(IBateRepository<ProductManage> prdto, ILogger<ProductListHandler> logger)
        {
            this.prdto = prdto; 
            this.logger = logger;   
        }
        /// <summary>
        /// 物料列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<ResultsDTO<ProductManage>> Handle(ProductListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                DapperHelper helper = new DapperHelper();
                ProductManage proget = new ProductManage();
                ResultsDTO<ProductManage> product = new ResultsDTO<ProductManage> { Code = 0 };
                string conn = helper.MangoMesWrite;
                string sql = "SELECT * from productManage";
                product.Data = prdto.GetAll(sql, proget, conn).ToList();
                logger.LogInformation("成功");
                return product;
            }
            catch (Exception ex)
            {
                logger.LogError("失败"+ex);
                throw;
            }


       
        }
    }
}
